package com.hongdee.rbac.res.controller;

import com.hongdee.base.entity.ResultEntity;
import com.hongdee.common.util.EntityUtils;
import com.hongdee.rbac.res.entity.TBcResource;
import com.hongdee.rbac.res.service.ITBcResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by My on 2017-01-11.
 */

@RestController
@RequestMapping("/rbac/res")
//@RequestMapping("/scm/dic/vendor")
public class TBcResourceController {

    @Autowired
    private ITBcResourceService tBcResourceService;


    /**
     * 方法名称:save
     * 方法描述:保存供应商数据
     * 返回内容:
     * 异常说明:
     * 创建用户:
     * 创建时间:
     * 修改用户:
     * 修改时间:
     * 修改内容:
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody TBcResource tBcResource) throws Exception {
        EntityUtils.valid(tBcResource);
        tBcResourceService.save(tBcResource);
        ResultEntity res = new ResultEntity();
        res.setMsg("保存成功！");
        res.setSuccess(true);
        res.setData(tBcResource);
        return res;
    }

    /**
     * 方法名称:  start
     * 方法描述:  根据主键 启用资源
     * 返回内容:ResultEntity 有效属性msg/success
     * 异常说明:
     * 创建用户:
     * 创建时间:
     * 修改用户:
     * 修改时间:
     * 修改内容:
     */
    @RequestMapping("/start")
    @ResponseBody
    public ResultEntity start(@RequestParam(name = "uid", defaultValue = "") String uid,
                              @RequestParam(name = "token", defaultValue = "") String token,
                              @RequestParam(name = "id", defaultValue = "") String id) throws Exception {

        this.tBcResourceService.update(id, "1");
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("启用成功");
        return res;
    }

    /**
     * 方法名称:  stop
     * 方法描述:  根据主键停用资源
     * 返回内容:ResultEntity 有效属性msg/success
     * 异常说明:
     * 创建用户:
     * 创建时间:
     * 修改用户:
     * 修改时间:
     * 修改内容:
     */
    @RequestMapping("/stop")
    @ResponseBody
    public ResultEntity stop(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {

        this.tBcResourceService.update(id, "0");
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("停用成功");
        return res;
    }

    /**
     * 方法名称:info
     * 方法描述:通过 ID 查询实体信息
     * 返回内容:ResultEntity 封装类,msg\success\data属性有效
     * 异常说明:
     * 创建用户:
     * 创建时间:
     * 修改用户:
     * 修改时间:
     * 修改内容:
     */
    @RequestMapping("/info")
    @ResponseBody
    public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        if (StringUtils.isEmpty(id)) {
            throw new Exception("请选择资源");
        }
        Map map = tBcResourceService.info(id);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }

    /**
     * 方法名称: resTree
     * 方法描述: 查询资源树
     * 参数名称:
     * 返回内容: 无
     * 异常说明: 抛出异常Exception
     * 创建用户: MissJin
     * 创建时间: 2017/1/21/17:23
     * 修改用户:
     * 修改时间:
     * 修改内容:
     */
    @RequestMapping("/resTree")
    @ResponseBody
    public ResultEntity resTree(@RequestParam(name = "uid", defaultValue = "") String uid,
                                @RequestParam(name = "token", defaultValue = "") String token,
                                @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        List list = this.tBcResourceService.tree(id);
        res.setDataList(list);
        res.setSuccess(true);
        res.setMsg("查询资源树");
        return res;
    }

    /**
     * 方法名称: resTreeChild
     * 方法描述: 查询资源下一级菜单
     * 参数名称:
     * 返回内容: 无
     * 异常说明: 抛出异常Exception
     * 创建用户: MissJin
     * 创建时间: 2017/1/21/17:23
     * 修改用户:
     * 修改时间:
     * 修改内容:
     */
    @RequestMapping("/resTreeChild")
    @ResponseBody
    public ResultEntity resTreeChild(@RequestParam(name = "uid", defaultValue = "") String uid,
                                     @RequestParam(name = "token", defaultValue = "") String token,
                                     @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        List list = this.tBcResourceService.findTreeChild(id);
        res.setTotal(Long.valueOf(list.size()));
        res.setDataList(list);
        res.setSuccess(true);
        res.setMsg("查询资源下级菜单成功");
        return res;
    }

    //

    /**
     * 方法名称: userResTree
     * 方法描述: 用户分配资源树
     * 参数名称:
     * 返回内容: 无
     * 异常说明: 抛出异常Exception
     * 创建用户: MissJin
     * 创建时间: 2017/1/21/17:23
     * 修改用户:
     * 修改时间:
     * 修改内容:
     */
    @RequestMapping("/userResTree")
    @ResponseBody
    public ResultEntity userResTree(@RequestParam(name = "uid", defaultValue = "") String uid,
                                     @RequestParam(name = "token", defaultValue = "") String token,
                                     @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        List list = this.tBcResourceService.userResTree(id,uid);
        res.setDataList(list);
        res.setSuccess(true);
        res.setMsg("查询资源树");
        return res;
    }

}
